package com.smalls.newvideotwo;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.MeasureUtil;
import com.uking.helper.VKNetBossUtils;
import com.uking.util.VKConstant;
import com.uking.ukingmodule.ukingjnic;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by hs on 2017/11/7.
 */

public class NewDTVApp extends Application {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + NewDTVApp.class.getSimpleName():NewDTVApp.class.getSimpleName();
    private static NewDTVApp newDTVApp;
    public static String mEpgServer = "";
    public static String mLastEpgServer = "";//最后一个epg
    public static List<String> mEpgServerList;// epgip 集合
    private static int mEpgServerCurrent = 0;

    // AAA注册信息属性文件存储对象
    public static SharedPreferences sharedPre;
    public static SharedPreferences.Editor editor;

    private String Token=null;

    public static String mPackageName = "";

    public String getToken() {
        return Token;
    }

    public void setToken(String token) {
        Token = token;
    }

    public static NewDTVApp getApp() {
        if (newDTVApp == null) throw new IllegalArgumentException("newDTVApp is null..");
        return newDTVApp;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        newDTVApp = this;
        MeasureUtil.init(getApplicationContext());
        x.Ext.init(getApp());

        loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);


        sharedPre = this.getSharedPreferences(Constant.TAG_CONFIG,
                Context.MODE_PRIVATE);

        editor = sharedPre.edit();
        if(Token==null) {
            Token = VKNetBossUtils.decrypt(NewDTVApp.getApp().sharedPre.getString(VKConstant.AUTHSTRING_REGISTER,""), Constant.KEY);
        }
    }

    public static String getmPackageName() {
        if (mPackageName != null && !TextUtils.isEmpty(mPackageName)) {
            return mPackageName;
        }
        String packageName = NewDTVApp.getApp().getApplicationContext().getPackageName();
        mPackageName = packageName;
        return packageName;
    }

    public HttpLoggingInterceptor loggingInterceptor;

    public static List<String> getmEpgServerList(){
        if(mEpgServerList != null && mEpgServerList.size() > 0){
            return mEpgServerList;
        }else {
            parseEpgserver(VKNetBossUtils.decrypt(sharedPre.getString(
                    Constant.EPGSERVER_REGISTER, ""),Constant.KEY));
            if(mEpgServerList != null && mEpgServerList.size() > 0){
                return mEpgServerList;
            }else {
                throw new IllegalArgumentException("mEpgServerList is empty");
            }
        }
    }

    public static void parseEpgserver(String mEpgip){
        LogUtil.d(TAG,"mEpgip="+mEpgip);
        String[] arrResponse = mEpgip.split(",");
        if(arrResponse != null && arrResponse.length > 0){
            mEpgServerList = new ArrayList<String>();
            for(String content : arrResponse){
                if(content!=null) {
                    mEpgServerList.add(content);
                }
            }
            Log.d(TAG,"mEpgServerList.size"+mEpgServerList.size()+"--arrResponse.length="+arrResponse.length);
        }else{
            mEpgServer=mEpgip;
            Log.d(TAG,"mEpgServerList.size=0");
        }
    }

    public static void setmEpgServer() {
        if(mEpgServer != null && !TextUtils.isEmpty(mEpgServer)){
            mLastEpgServer = mEpgServer;
        }else if(mEpgServerList != null && mEpgServerList.size() > 0){
            mEpgServer = mEpgServerList.get(0);
            mEpgServerCurrent = 0;
            mLastEpgServer = mEpgServerList.get(mEpgServerList.size()-1);
        }else {
            parseEpgserver(VKNetBossUtils.decrypt(sharedPre.getString(
                    Constant.EPGSERVER_REGISTER, ""),Constant.KEY));
            if(mEpgServerList == null || mEpgServerList.size() <= 0){
                throw new IllegalArgumentException("mEpgServerList is empty");
            }else {
                mEpgServerCurrent = 0;
                mEpgServer = mEpgServerList.get(mEpgServerCurrent);
                mLastEpgServer =  mEpgServerList.get(mEpgServerList.size()-1);
            }
        }
    }

    public static String getmEpgServer() {
        if(mEpgServer != null && !TextUtils.isEmpty(mEpgServer)){
            //LogUtil.i(TAG, "mEpgServer = " + mEpgServer);
            return mEpgServer;
        }
        if(mEpgServerList != null && mEpgServerList.size() > 0){
            LogUtil.i(TAG, "mEpgServerList.get(0) = " + mEpgServerList.get(mEpgServerCurrent));
            mEpgServer = mEpgServerList.get(mEpgServerCurrent);
            return mEpgServer;
        }else{
            parseEpgserver(VKNetBossUtils.decrypt(sharedPre.getString(
                    Constant.EPGSERVER_REGISTER, ""),Constant.KEY));
            if(mEpgServerList != null && mEpgServerList.size() > 0){
                LogUtil.i(TAG, "else mEpgServerList.get(0) = " + mEpgServerList.get(0));
                mEpgServerCurrent = 0;
                mEpgServer = mEpgServerList.get(mEpgServerCurrent);
                return mEpgServer;
            }else {
                throw new IllegalArgumentException("mEpgServerList is empty");
            }
        }
    }


    public static Boolean isLastServer(){
        if(mLastEpgServer != null && !TextUtils.isEmpty(mLastEpgServer)){
            if(mEpgServerList==null||mEpgServerList.size() <= 1){
                return true;
            }else if(mLastEpgServer.equals(mEpgServer)){
                return true;
            }
        }
        return false;
    }

    public static void setmEpgServerNext() {
        if(mEpgServerList == null || mEpgServerList.size() == 0){
            parseEpgserver(VKNetBossUtils.decrypt(sharedPre.getString(
                    Constant.EPGSERVER_REGISTER, ""),Constant.KEY));
        }
        int i = mEpgServerCurrent + 1 >= mEpgServerList.size() ?
                0 : mEpgServerCurrent + 1;
        mEpgServer = mEpgServerList.get(i);
        LogUtil.d(TAG,"setmEpgServerNext="+mEpgServer);
    }

}
